﻿Public Class DatHangChiTietDA
    Private cn As DatabaseConnect
    Private sql As String

    Public Sub New()
        cn = New DatabaseConnect()
    End Sub

    Public Function InUpDHCT(ByVal dhctDT As DatHangChiTietDT, ByVal flag As Boolean)
        If flag = True Then
            sql = "INSERT INTO SanPham_DatHang_ChiTiet  VALUES('" + dhctDT.Ma + "'," & dhctDT.STT & ", " & dhctDT.MaLoai & ", " & dhctDT.SoLuong & ", N'" + dhctDT.GhiChu + "'," & dhctDT.ThanhTien & ")"
        Else
            sql = "UPDATE SanPham_DatHang_ChiTiet SET thanhtien = " & dhctDT.ThanhTien & ",MaLoai = " & dhctDT.MaLoai & ", soluong = " & dhctDT.SoLuong & ",ghichu=  N'" + dhctDT.GhiChu + "' WHERE MaPhieu = '" + dhctDT.Ma + "' AND STT = " & dhctDT.STT
        End If
        Return cn.Execute(sql)
    End Function

    Public Function DelDHCT(ByVal dhctDT As DatHangChiTietDT)
        sql = "DELETE FROM SanPham_DatHang_ChiTiet WHERE MaPhieu = '" + dhctDT.Ma + "' AND STT = " & dhctDT.STT
            Return cn.Execute(sql)
    End Function

    Public Function GetData(ByVal dieukien As String) As List(Of DatHangChiTietDT)
        Dim dh As DatHangChiTietDT
        Dim list As New List(Of DatHangChiTietDT)
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM SanPham_DatHang_ChiTiet " + dieukien)

        For i As Integer = 0 To dt.Rows.Count - 1
            Dim ma As String = dt.Rows(i)("MaPhieu").ToString()
            Dim stt As Integer = Convert.ToInt32(dt.Rows(i)("STT"))
            Dim maloai As Integer = Convert.ToInt32(dt.Rows(i)("MaLoai"))
            Dim soluong As Double = Convert.ToDouble(dt.Rows(i)("soluong"))
            Dim ghichu As String = dt.Rows(i)("ghichu").ToString()
            Dim thanhtien As Double = Convert.ToDouble(dt.Rows(i)("thanhtien"))
            '-----'
            dh = New DatHangChiTietDT(ma, maloai, stt, soluong, ghichu, thanhtien)
            list.Add(dh)
        Next
        Return list
    End Function

    Public Function GetSTT(ByVal ma As String) As Integer
        Dim stt As Integer = 1
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM SanPham_DatHang_ChiTiet WHERE MaPhieu = '" + ma + "'")
        If dt.Rows.Count > 0 Then
            stt = dt.Rows.Count + 1
        End If
        Return stt
    End Function
End Class
